package com.douglas.managesystem.service.system.impl;

import com.douglas.managesystem.common.SearchEntity;
import com.douglas.managesystem.dao.student.ClassMapper;
import com.douglas.managesystem.dao.student.InstructMapper;
import com.douglas.managesystem.dao.student.LessonMapper;
import com.douglas.managesystem.dao.student.TeacherMapper;
import com.douglas.managesystem.entity.studentpojo.Classpojo;
import com.douglas.managesystem.entity.studentpojo.Instruct;
import com.douglas.managesystem.entity.studentpojo.Lesson;
import com.douglas.managesystem.entity.studentpojo.Teacher;
import com.douglas.managesystem.service.system.InstructService;
import com.douglas.managesystem.vo.InstructVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 授课管理
 * @Author 以梦为馬 <Douglas1314@126.com>
 * @Version V1.0.0
 * @Date 2020-11-21 0:05
 **/
@SuppressWarnings("all")
@Service
public class InstructServiceImpl implements InstructService {

    @Autowired
    private InstructMapper instructMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private ClassMapper classMapper;

    @Autowired
    private LessonMapper lessonMapper;
    /**
     * 1.分页查询授课管理信息
     * @return
     */
    @Override
    public PageInfo<InstructVO> queryListByPage(SearchEntity params) {
        if(params.getPageNum() == null || params.getPageNum() == 0) {
            params.setPageNum(1);
        }
        if(params.getPageSize() == null || params.getPageSize() == 0) {
            params.setPageSize(10);
        }
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<Instruct> InstructList = instructMapper.queryListByPage(params);
        ArrayList<InstructVO> instructVOList = new ArrayList<>();
        for (Instruct instruct : InstructList) {
            InstructVO instructVO = new InstructVO();
            Teacher teacher = teacherMapper.queryTeacherByTeacherId(instruct.getTeacherId());
            Classpojo classpojo = classMapper.queryClassPoByClassId(instruct.getClassId());
            Lesson lesson = lessonMapper.queryLessonByLessonId(instruct.getLessonId());
            instructVO.setId(instruct.getId());
            instructVO.setClassId(instruct.getClassId());
            instructVO.setClassName(classpojo.getClassName());
            instructVO.setTeacherId(instruct.getTeacherId());
            instructVO.setTeacherName(teacher.getName());
            instructVO.setLessonId(instruct.getLessonId());
            instructVO.setLessonName(lesson.getName());
            instructVO.setStartSemester(lesson.getStartSemester());
            instructVO.setCreatedBy(instruct.getCreatedBy());
            instructVO.setCreatedTime(instruct.getCreatedTime());
            instructVO.setUpdatedBy(instruct.getUpdatedBy());
            instructVO.setUpdatedTime(instruct.getUpdatedTime());
            instructVOList.add(instructVO);
        }
        PageInfo<InstructVO> pageInfo = new PageInfo<>(instructVOList);
        return pageInfo;
    }

    /**
     * 2.新增授课对象信息
     * @param instruct
     */
    @Override
    public void saveInstruct(Instruct instruct) {
        Instruct instructDb = instructMapper.queryInstruct(instruct);
        if(instructDb != null){
            return;
        }
        instructMapper.insert(instruct);
    }

    /**
     * 3.更新授课对象信息
     * @param instruct
     */
    @Override
    public void updateInstruct(Instruct instruct) {
        instructMapper.update(instruct);
    }

    /**
     * 4.根据id查询授课对象
     * @param id
     * @return
     */
    @Override
    public Instruct findByInstructId(String id) {
        return instructMapper.queryByInstructId(id);
    }

    /**
     * 5.根据id删除授课信息
     * @param id
     */
    @Override
    public void deleteById(String id) {
        instructMapper.delete(id);
    }
}
